#!/usr/bin/env python3
"""
Meson MCP Server

面向 meson 构建系统的 MCP server，提供 meson 命令行工具的封装。

MCP tools 列表：
- meson_wrap_help: 显示 meson wrap 命令的帮助信息
- meson_wrap_list_help: 显示 meson wrap list 命令的帮助信息
- meson_wrap_search_help: 显示 meson wrap search 命令的帮助信息
- meson_wrap_install_help: 显示 meson wrap install 命令的帮助信息
- meson_wrap_update_help: 显示 meson wrap update 命令的帮助信息
- meson_wrap_info_help: 显示 meson wrap info 命令的帮助信息
- meson_wrap_status_help: 显示 meson wrap status 命令的帮助信息
- meson_wrap_promote_help: 显示 meson wrap promote 命令的帮助信息

所有工具返回统一的 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

import subprocess
import json
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("Meson MCP Server")

@mcp.tool()
def meson_wrap_help() -> dict:
    """
    显示 meson wrap 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_list_help() -> dict:
    """
    显示 meson wrap list 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "list", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap list --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap list --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_search_help() -> dict:
    """
    显示 meson wrap search 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "search", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap search --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap search --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_install_help() -> dict:
    """
    显示 meson wrap install 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "install", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap install --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap install --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_update_help() -> dict:
    """
    显示 meson wrap update 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "update", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap update --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap update --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_info_help() -> dict:
    """
    显示 meson wrap info 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "info", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap info --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap info --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_status_help() -> dict:
    """
    显示 meson wrap status 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "status", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap status --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap status --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def meson_wrap_promote_help() -> dict:
    """
    显示 meson wrap promote 命令的帮助信息
    
    返回:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["meson", "wrap", "promote", "--help"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "meson wrap promote --help",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "meson wrap promote --help",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()